//
//  XJTextView.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/26.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

class XJTextView: UITextView {
    
    
    var placeholder: String?{
    
        didSet{
        
        placeholderLabel.text = placeholder
        }
    }
    
   override var font: UIFont?{
    
        didSet{
        
        placeholderLabel.font = font
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(self).offset(-10)
        }
        
        //通知监听textView改变,隐藏placeholderLabel
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChanged", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
   @objc private func textDidChanged() {
        placeholderLabel.hidden = hasText()
    
    }
    //移除监听
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    private lazy var placeholderLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 14)
        label.text = "分享新鲜事..."
        label.numberOfLines = 0
        
        return label
    
    }()

}
